package helper

import (
	"crypto/tls"
	"crypto/x509"
	"google.golang.org/grpc/credentials"
	"io/ioutil"
)

func GetServerCerts() credentials.TransportCredentials {
	certFile, _ := tls.LoadX509KeyPair("src/grpc/cert/server.pem", "src/grpc/cert/server.key")
	/*if err != nil {
		logging.Error("credentials.NewServerTLSFromFile error is ", err)
		return
	}*/
	certPool := x509.NewCertPool()
	ca, _ := ioutil.ReadFile("src/grpc/cert/ca.pem")
	certPool.AppendCertsFromPEM(ca)

	crets := credentials.NewTLS(&tls.Config{
		Certificates: []tls.Certificate{certFile},
		ClientAuth:   tls.RequireAndVerifyClientCert,
		ClientCAs:    certPool,
	})
	return crets
}

func GetClientCerts() credentials.TransportCredentials {
	certFile, _ := tls.LoadX509KeyPair("src/grpc/cert/client.pem", "src/grpc/cert/client.key")
	/*if err != nil {
		logging.Error("credentials.NewServerTLSFromFile error is ", err)
		return
	}*/
	certPool := x509.NewCertPool()
	ca, _ := ioutil.ReadFile("src/grpc/cert/ca.pem")
	certPool.AppendCertsFromPEM(ca)

	crets := credentials.NewTLS(&tls.Config{
		Certificates: []tls.Certificate{certFile},
		ServerName:   "localhost",
		RootCAs:      certPool,
	})

	return crets

}
